<?php
namespace app\controllers;

use app\models\eventType;

class EventController extends \app\common\BaseController{
    public function actionIndex(){
        $res = \app\models\eventType::find()->asArray()->all();
        return $this->result($res);
    }

    public function actionAdd(){
        $postData = \Yii::$app->request->post();
        if (isset($postData['id']) && !empty($postData['id'])){
            $eventType = eventType::find()->where(['id' => $postData['id']])->one();
        }else{
            $eventType = new eventType();
        }
        $eventType->name = $postData['name'];
        $eventType->rgb = $postData['rgb'];
        $eventType->ctime = time();
        $code = 1;
        if ($eventType->save()){
            $code = 0;
        }
        return $this->result([], $code);
    }

    public function actionDetail(){
        $id = \Yii::$app->request->get('id', 0);
        $typeInfo = [];
        if ($id){
            $typeInfo = eventType::find()->where(['id' => $id])->asArray()->one();
        }
        return $this->result($typeInfo);
    }

    public function actionDisable(){
        $eventId = \Yii::$app->request->get('id');
        $code = 1;
        if ($eventId){
            $eventObj = eventType::find()->where(['id' => $eventId])->one();
            if ($eventObj){
                $eventObj->isDisabled = !$eventObj->isDisabled;
                if ($eventObj->save()){
                    $code = 0;
                }
            }
        }
        return $this->result([], $code);
    }
}